package com.zhiyou100.crm.dao.impl;

import com.zhiyou100.crm.dao.DepartmentDao;
import com.zhiyou100.crm.util.DBUtil1;
import com.zhiyou100.crm.util.Department;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDaoImpl implements DepartmentDao {
    DBUtil1 dbUtil1=new DBUtil1();
    ResultSet rs;

    @Override
    public List<Department> queryAll() {
        List<Department> departments=new ArrayList<>();
        String sql="select * from department";
        rs=dbUtil1.select(sql,null);
        try {
            while (rs.next()) {
                int department_id = rs.getInt("department_id");
                String department_name = rs.getString("department_name");
                String department_desc = rs.getString("department_desc");
                String remark = rs.getString("remark");
                Timestamp update_time = rs.getTimestamp("update_time");
                Department department=new Department(department_id,department_name,department_desc,remark,update_time);
                departments.add(department);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            dbUtil1.close();
        }

        return departments;
    }

    @Override
    public int addDepartment(Department department) {
        String sql="insert into department(department_name,department_desc,update_time) values(?,?,?)";
        Object[] obj={department.getDepartment_name(),department.getDepartment_desc(),department.getUpdate_time()};
        return dbUtil1.update(sql,obj);
    }

    @Override
    public int deleteById(int departmentId) {
        String sql="delete from department where department_id=?";
        Object[] obj={departmentId};
        return dbUtil1.update(sql,obj);
    }

    @Override
    public int updateDepartment(Department department) {
        String sql="update department set department_name=?,department_desc=?,update_time=? where department_id=?";
        Object[] obj={department.getDepartment_name(),department.getDepartment_desc(),department.getUpdate_time(),department.getDepartment_id()};
        return dbUtil1.update(sql,obj);

    }
}
